<template>
  <div class="app-container">
    <div class="channel_tree_div">
      <el-tree
        :data="channelTree"
        node-key="id"
        default-expand-all
        :expand-on-click-node="false">
      <span class="custom-tree-node" slot-scope="{ node, data }">
        <span >{{ data.name }}</span>
        <span style="margin-left: 30px">
          <el-button
            v-if="data.level ===1 || data.level ===2"
            type="text"
            size="mini"
            @click="() => append(data)">
            添加下级
          </el-button>
          <el-button
            v-if="data.children ===undefined || data.children.length===0"
            type="text"
            size="mini"
            @click="() => handleDelete(data)">
            删除
          </el-button>
        </span>
      </span>
      </el-tree>
    </div>

  </div>
</template>
<style>
  .channel_tree_div{
    width: 30%;
    padding: 30px;
    border: 2px solid black;
    /*height: 1000px;*/
  }


</style>

<script>
import { getAllGoodsChannel,delChannel } from "@/api/smy/goodsChannel";
import {handleTree} from "../../utils/ruoyi";

export default {
  name: "User",
  data() {
    return {
      channelTree:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      getAllGoodsChannel().then(response => {
        const channelList = response.data || [];
        let channelTree = handleTree(channelList,"id","parent");
        this.channelTree = channelTree;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加合作商家";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getUser(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商家信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleUpgrade(row){
      const id = row.id;
      let optDto = {optId:id}
      this.$modal.confirm('是否确认将手机号码为"' + row.mobile + '"的用户升级为加盟商？').then(function() {
        return upgradeUser(optDto);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {});
    },
    handleDisable(row){
      const id = row.id;
      let optDto = {optId:id,optName:0}
      this.$modal.confirm('是否确认停用名称为"' + row.shopname + '"的商家账号？').then(function() {
        return changeStatus(optDto);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {});
    },
    handleEnable(row){
      const id = row.id;
      let optDto = {optId:id,optName:1}
      this.$modal.confirm('是否确认启用名称为"' + row.shopname + '"的商家账号？').then(function() {
        return changeStatus(optDto);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {});
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id;
      this.$modal.confirm('是否确认删除名称为"' + row.name + '"的商品类目？').then(function() {
        return delChannel(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
};
</script>
